//
//  IntroAPPUtil.m
//  IntroductionApp
//
//  Created by brave on 11/08/10.
//  Copyright 2011 __MyCompanyName__. All rights reserved.
//

#import "IntroAPPUtil.h"


@implementation IntroAPPUtil

/* NSDate */

// Twitterの「CreateAt」をNSDataに変換
+(NSDate*)dateFromCreatedAtDateString:(NSString*)strDate {
    
    // Setting DateFormatter
    NSDateFormatter* format = nil;
    if (format == nil) {
        format = [[NSDateFormatter alloc] init];
        NSLocale *locale = [[NSLocale alloc] initWithLocaleIdentifier:@"en_US"];
        [format setLocale:locale];
        [format setDateStyle:NSDateFormatterLongStyle];
        [format setTimeStyle:NSDateFormatterNoStyle];
        [format setDateFormat:@"EEE MMM dd HH:mm:ss Z yyyy"];
    }
    NSDate *date = [format dateFromString:strDate];
    return date;
}

// NSDataを指定フォーマットでNSStringに変換
+(NSString*)NSDateToFormatString:(NSDate*)date{
    NSDateFormatter *formatter = [[NSDateFormatter alloc] init];
    [formatter setDateFormat:@"yyyy/MM/dd HH:mm:ss"];
    NSString *str = [formatter stringFromDate:date];
    
    [ formatter release ];
    
    return str;
}


/* UILabel */

// フォントサイズとラベルの横幅から、UILabelの適切なサイズ返す
+(float)textHeight:(NSString*)text fontSize:(float)fsize labelWidth:(float)Lwidth{
	CGSize boundingSize = CGSizeMake(Lwidth, CGFLOAT_MAX);
	//文字の横幅から高さを算出
	CGSize labelsize = [text sizeWithFont:[UIFont systemFontOfSize:fsize]
                        constrainedToSize:boundingSize
                            lineBreakMode:UILineBreakModeWordWrap];
	return labelsize.height;
}

/* save File */
+(void)saveFile:(NSData*)fileData{
    NSString *fileName = [ NSString stringWithFormat:@"~/Documents/%@",NewsFile ];
    [fileData writeToFile:[fileName stringByExpandingTildeInPath] atomically:YES];
}

/* file */
//Resources内のファイルパスを取得する
+(NSString*)getResourcesPath:(NSString*)filePath{
    return [[[NSBundle mainBundle] bundlePath] stringByAppendingPathComponent:filePath  ];
}

//アプリのDocumentsのパスを取得する
+(NSString*)getDocumentsPath:(NSString*)fileName{
    return [ NSString stringWithFormat:@"%@/Documents/%@",NSHomeDirectory(),fileName ];
}

+(NSString*)getCachePath:(NSString*)fileName{
    NSArray *paths = NSSearchPathForDirectoriesInDomains(NSCachesDirectory , NSUserDomainMask, YES);
    NSString *pathStr = [paths objectAtIndex:0];
    return [ NSString stringWithFormat:@"%@/%@",pathStr,fileName ];
}

+(NSString*)getMusicFilesFolderPath{
    NSString *path = [self getDocumentsPath:kMusicFileFolder];
    if ([[NSFileManager defaultManager] fileExistsAtPath:path] == FALSE) {
        [[NSFileManager defaultManager] createDirectoryAtPath:path withIntermediateDirectories:NO attributes:nil error:nil];
    }
    return path;
}
/* plist */
+(NSArray*)readPlist:(NSString*)listName{
    //NSBundle* bundle = [NSBundle mainBundle];
    //NSString* path = [bundle pathForResource:listName ofType:@"plist"];
    
    NSString *path = [ IntroAPPUtil getDocumentsPath:listName ];
    NSArray* ary = [NSArray arrayWithContentsOfFile:path];
    
    return ary;
}


+(NSDictionary*)readDicPlist:(NSString*)listPath{

    NSDictionary* dic = [NSDictionary dictionaryWithContentsOfFile:listPath];
    
    return dic;
}

/* =====================================================================================  */
#pragma mark - network
/* =====================================================================================  */

// 通信できるか
+(BOOL)isNetworkState{
    // インターネット状態をチェック、アクセスできない場合は、アラートを出す。
    Reachability *reach = [Reachability reachabilityForInternetConnection];
    if (reach.currentReachabilityStatus == NotReachable) {
        return FALSE;
    }
    return TRUE;
}



@end
